package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class ProveedoresPreferenciales extends Activity {
	private ArrayList<Dictionary> Actividades;

	private String titulo;
	private String tvTitulo;

	private ListView list;

	public ProgressDialog progressDialog;

	public Contenidos_Adapter adapter;

	private Intent intent;

	public void voidFinish(View V) {
		finish();

	}

	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		// Checks the orientation of the screen

	}

	public void abrirMenuLateral(View V) {
		intent = new Intent(ProveedoresPreferenciales.this, MenuLateral.class);

		startActivity(intent);
	}

	@Override
	public void onBackPressed() {
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_proveedorespreferenciales);

		// /** Quitamos las opciones 1 y 2
		// *
		// */
		// String qry_url = getString(R.string.QRY_URL)
		// + Uri.encode("select * from categorias where cat_cve>2");
		//
		//
		//
		// saxXML saxparser = new saxXML(qry_url);
		//
		//
		// Actividades = saxparser.parse();
		//
		// list = (ListView) findViewById(R.id.list);
		//
		// Contenidos_Adapter adapter = new Contenidos_Adapter(Actividades);
		//
		//
		// list.setAdapter(adapter);

		CargarContenido();

	}

	private void CargarContenido() {
		// TODO Auto-generated method stub
		new AsyncActividades().execute("contenido");

	}

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {

		protected void onPostExecute(Integer in) {

			list = (ListView) findViewById(R.id.list);
			adapter = new Contenidos_Adapter(Actividades);
			list.setAdapter(adapter);

			list.setTextFilterEnabled(true);

			list.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					if (Actividades.get(position).get("cat_cve").toString()
							.equals("2")) {
						// Restaurantes
						intent = new Intent(ProveedoresPreferenciales.this,
								Restaurantes.class);
						intent.putExtra("PUT_TITULO", "Restaurantes");
						startActivity(intent);

					} else if (Actividades.get(position).get("cat_cve")
							.toString().equals("1")) {
						// Hoteles
						intent = new Intent(ProveedoresPreferenciales.this,
								Hoteles.class);
						intent.putExtra("PUT_TITULO", "Hoteles");
						startActivity(intent);

					} else {

						Intent intent = new Intent(
								ProveedoresPreferenciales.this,
								ProveedoresPreferencialesDetalle.class);

						// clsDictionary.setDctionary(Actividades.get(position));

						intent.putExtra("putTITULO", Actividades.get(position)
								.get("cat_des").toString());

						intent.putExtra("putCAT_CVE", Actividades.get(position)
								.get("cat_cve").toString());
						startActivity(intent);
						// TODO: aqui poner la validadcion de que se valla a
						// otra
						// ventana Restaurantes o Hoteles.
						// finish();
					}
				}
			});
			progressDialog.dismiss();
		}

		protected void onPreExecute() {

			progressDialog = ProgressDialog.show(
					ProveedoresPreferenciales.this, "", "Cargando...");
		}

		protected Integer doInBackground(String... urls) {

			String qry_url = getString(R.string.QRY_URL)
					+ Uri.encode("select * from categorias ");

			saxXML saxparser = new saxXML(qry_url);

			Actividades = saxparser.parse();

			return null;

		}

	}

	public class Contenidos_Adapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		private ArrayList<Dictionary> data;

		public Contenidos_Adapter(ArrayList<Dictionary> d) {

			data = d;
			inflater = (LayoutInflater) ProveedoresPreferenciales.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		}

		public int getCount() {
			return data.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public class ViewHolder {
			public TextView tv_todocontenido;

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			View vi = convertView;
			ViewHolder holder;

			if (convertView == null) {

				vi = inflater.inflate(
						R.layout.ui_proveedorespreferenciales_items, null);

				holder = new ViewHolder();

				holder.tv_todocontenido = (TextView) vi
						.findViewById(R.id.tv_proveedorespreferenciales_item);

				vi.setTag(holder);

			} else

				holder = (ViewHolder) vi.getTag();

			holder.tv_todocontenido.setText(data.get(position).get("cat_des")
					.toString());
			return vi;

		}

	}

}
